package com.example.mychatroom.controller;

import com.example.mychatroom.model.User;
import com.example.mychatroom.serviceApi.UserService;
import com.example.mychatroom.a_utils.ResponseBodyMessage;
import com.example.mychatroom.a_utils.SessionKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    public UserService userService;


    @RequestMapping("/subLogin")
    public ResponseBodyMessage<User> login(HttpServletRequest request,
                                           @RequestParam String account,
                                           @RequestParam String password){
        // @RequestParam String verifyCode
        ResponseBodyMessage<User> responseBodyMessage = new ResponseBodyMessage<>();
        boolean flag = userService.login(request,account,password,responseBodyMessage);
        if (flag){
            //如果登录成功，则创建Session会话
            User user = responseBodyMessage.getData();
            HttpSession session = request.getSession();
            session.setAttribute(SessionKey.USERINFO_SESSION_KEY,user);
        }
        return responseBodyMessage;
    }

//    @RequestMapping("/verifyCode")
//    public void generateImage(HttpServletRequest request, HttpServletResponse response){
//        userService.buildCheckImage(request,response);
//    }

    public void logout(String account,String password){
        System.out.println("退出登录成功");
    }

}
